package nju.mscs.businesslogic;
import java.util.*;
import nju.mscs.data.object.*;
import nju.mscs.test.SellGoodsTest;


public class SG implements SellGoods{
  private ArrayList<Integer> numberList=new ArrayList<Integer>();//表示商品买卖数量
  private double price;
  private int sellingID;//销售号
  private double change;//找零
  private ArrayList<Goods> sellingList=new ArrayList<Goods>();//商品清单
  private ArrayList<GoodsDiscountPromotion> discountList=new ArrayList<GoodsDiscountPromotion>();//策略清单
  private ArrayList<GiftPromotion> giftList=new ArrayList<GiftPromotion>();
  private VipConsumer vip;//VIP
  private Bill bill;
  private User cashier;
  private int usingPoints;
  private Database db;
	public SG(){
		db=LogIn.db;
		cashier=LogIn.user;
	}
	public int useVIP(String id){
	    vip=db.searchVipConsumerById(id);
	    if(vip==null)
	    	return -1;
	    else
	    	return vip.getRestPoints();
	}
	public String[] addGoods(String id,int n){//传入商品+数量
		Goods goods=db.searchGoodsById(id);
		if(goods!=null){
			sellingList.add(goods);
	 		numberList.add(n);
	 		Iterator<DiscountPromotion> idp=null;
	 		Iterator<GiftPromotion> igp=null;
			GoodsDiscountPromotion dp=null;
			GiftPromotion gp=null;
			String dis=null;
			String gift=null;
	 		if((idp=db.getDiscountPromotionByGoods(id))!=null){
	 			while(idp.hasNext()){
					dp=(GoodsDiscountPromotion)idp.next();
					discountList.add(dp);
	 			}
	 			dis=dp.getDiscount()*10+"折";
	 		}
	 		if((igp=db.getGiftPromotionByGoods(id))!=null){
	 			while(igp.hasNext()){
					gp=igp.next();
					giftList.add(gp);
	 			}
	 			gift=gp.getGift().getName();
	 		}
	 		String[] result={""+goods.getId(),goods.getName(),""+n,""+goods.getPrice(),dis,gift,""+Math.round(n*goods.getPrice()*dp.getDiscount()*100.0)/100.0};
	 		return result;
		}
		else
			return null;
	 }
		public void deleteGoods(String id,int n){//删除商品+数量
	   int i=0;
	   for(Goods goods:sellingList){
	   if(goods.getId()==id){
	   if(numberList.get(i)>n)
	   numberList.set(i, numberList.get(i)-n);
	   else
	   sellingList.remove(goods);
	   break;
	   }
	   i++;
	} 
	   }
	public double[] calculate(int point){
		usingPoints=point;
		double[] result=new double[3];
		double t=100;
		bill=db.createNewBill(createBillID(), vip, cashier);
		double cal=0;
		double[] dc=new double[sellingList.size()];
		for(int k=0;k<dc.length;k++){
			dc[k]=1;
		}
		int i=0;
		for(Goods goods:sellingList){
			for(GoodsDiscountPromotion pro:discountList){
				if(goods.equals(pro.getGoods())){
					dc[i]=pro.getDiscount();
					bill.addPromotion((Promotion)pro);
				}
			}
			cal+=goods.getPrice()*numberList.get(i)*dc[i];
			i++;
		}
		cal=Math.round(cal*t)/t;
		result[0]=cal;
		Iterator<DiscountPromotion>idp=null;
		DiscountPromotion dp=null;
		double discount=1;
		if((idp=db.getDiscountPromotionByTotal(cal))!=null){
			while(idp.hasNext()){
				dp=idp.next();
				bill.addPromotion((Promotion)dp);
				discount=dp.getDiscount();
			}
			cal=cal*discount;
		}
		cal=Math.round(cal*t)/t;
		result[1]=discount;
		result[2]=cal-point/50;
		price=cal;
		i=0;
		for(Goods goods:sellingList)
	    	db.sellGoods(goods, numberList.get(i), Math.round(discount*dc[i]*t)/t);
	    return result;
	}
	public double change(double money){
		change=money-price;
		change=Math.round(change*100.0)/100.0;
		return change;
	}
	public String[] getGift(){
		if(bill!=null){
			int k=0;
			String giftID=null;
			if(vip!=null){
				k=vip.getGift().size();
			}			
			int n=giftList.size();
			String result[]=new String[n+k];
			for(int j=0;j<k;j++){
				result[n+j]=db.searchGiftById(vip.getGift().get(j)).getName();
			}
			int i=0;
			for(GiftPromotion gift:giftList){
				result[i]=gift.getGift().getName();
				i++;
			}
		return result;
		}
		else 
		return null; 
	}
	public boolean store(){
		if(vip!=null){//用户积分更改
			vip.addRestPoints(-usingPoints);
			vip.addSumPoints((int)price/10);//价格/10
			db.requsetChangerVipPoint(vip.getId(), (int)price/10,-usingPoints);
			vip.InGift();
		}
		int i=0;
		for(Goods goods:sellingList){
			goods.setAmount(goods.getAmount()-numberList.get(i));
			i++;
		}
		takeReceipts();
	return true;
	}
	private String takeReceipts(){
		System.out.println(db.printBill());
	    return db.printBill();
	}
	@SuppressWarnings("static-access")
	private String createBillID(){
		Calendar ca=Calendar.getInstance();
		String id=""+LogIn.num+ca.DATE+ca.get(ca.HOUR_OF_DAY)+ca.get(ca.MINUTE);
		System.out.println(id+"BillID");
		return id;
	}
}